package com.leyou.second.controller;

import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.auth.pojo.UserInfo;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.second.dto.SkusDto;
import com.leyou.second.pojo.Sku;
import com.leyou.second.service.SecondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 秒杀控制器
 *
 * @Classname: [ SecondController ]
 * @Date: [ 2020/11/17 12:18 ]
 * @Author: by [ g_s ]
 * @Version: [v1.0]
 */
@Controller
public class SecondController {
    @Autowired
    private SecondService secondService;


    /**
     * 秒杀列表页数据
     *
     * @return
     */
    @GetMapping("/secondList")
    @ResponseBody
    public ResponseEntity<List<SkusDto>> getSkusList() {
        List<SkusDto> skusDtos = secondService.getSkusList();
        return ResponseEntity.ok(skusDtos);
    }

    /**
     * 秒杀详情请求
     * 路径： /second/100.html
     */
    @GetMapping("/seckills/{id}.html")
    public String seckill(@PathVariable("id") Long id, Model model) {
        //1.获取商品详情需要的数据
        Map<String, Object> resultMap = secondService.seckillSer(id);
        //2.把数据传递到页面
        model.addAllAttributes(resultMap);

        return "seckill-item";
    }

    /**
     * 秒杀请求
     */
    @PostMapping("/seckills/skill")
    @ResponseBody
    public ResponseEntity<Boolean> skill(@RequestBody Sku id) {
        Boolean aBoolean = secondService.skill(id.getId());
        return ResponseEntity.ok(aBoolean);
    }


}
